package com.mytaobao.main.cart;


//import static com.google.common.base.Preconditions.checkNotNull;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.baseproject.eventbus.EventBus;
import com.baseproject.eventbus.Subscribe;
import com.baseproject.eventbus.ThreadMode;
import com.baseproject.itemdecoration.DividerLinearItemDecoration;
import com.baseproject.mvp.MvpFragment;
import com.baseproject.util.LogUtils;
import com.jaeger.library.StatusBarUtil;
import com.mytaobao.R;
import com.mytaobao.detail.DetailActivity;
import com.mytaobao.main.cart.func.cart.ItemCommodity;
import com.mytaobao.main.cart.func.cart.SelectedChangeEvent;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 将View分离为基础方法和业务方法
 */
public class CartFragment extends MvpFragment<CartFragment, CartPresenter>
        implements CartContract.ICartView {

    @BindView(R.id.rv_cart)
    RecyclerView recyclerView;

    public static String TAG = "CartFragment";
    private CartRecyclerViewAdapter mCartRecyclerViewAdapter;
    @BindView(R.id.tvTitleCount)
    TextView tvTitleCount;

    @BindView(R.id.tvCountMoney)
    TextView tvCountMoney;

    @BindView(R.id.tvSettle)
    TextView tvSettle;

    @BindView(R.id.ivSelectAll)
    CheckBox ivSelectedAll;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_cart;
    }

    @NonNull
    @Override
    public CartPresenter createPresenter() {
        return new CartPresenter();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    protected void _onCreateView(View view, ViewGroup container, Bundle savedInstanceState) {
        //绑定fragment
        ButterKnife.bind( this, view ) ;
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.getContext(),LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        mCartRecyclerViewAdapter = new CartRecyclerViewAdapter();
        recyclerView.setAdapter(mCartRecyclerViewAdapter);

        //添加分割线
        recyclerView.addItemDecoration(new DividerLinearItemDecoration(
                getActivity(), DividerLinearItemDecoration.VERTICAL));

        onSelectedChangeEvent(new SelectedChangeEvent(0, 0));
        EventBus.getDefault().register(this);

        ivSelectedAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = ((CheckBox)v).isChecked();
                mCartRecyclerViewAdapter.updateAllSelected(isChecked);
                mCartRecyclerViewAdapter.notifyDataSetChanged();
                onSelectedChangeEvent(new SelectedChangeEvent(0, 0));
            }
        });
        ivSelectedAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                LogUtils.d("[onCheckedChanged] isChecked = " + isChecked);
            }
        });
    }

    private void updateSettleUI(SelectedChangeEvent event) {
        float priceSum = event.getSelectedPriceSum();
        int selectedCount = event.getSelectedCount();
        BigDecimal bg = new BigDecimal(priceSum).setScale(1, RoundingMode.HALF_DOWN);
        String selectMoney = String.format(getResources().getString(R.string.count_money), bg.floatValue() + "");
        String selectCount = String.format(getResources().getString(R.string.count_goods), selectedCount + "");
        tvCountMoney.setText(selectMoney);
        tvSettle.setText(selectCount);
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onSelectedChangeEvent(SelectedChangeEvent event) {
        LogUtils.d("[onSelectedChangeEvent] event = " + event);
        event = mCartRecyclerViewAdapter.getSelectedChangeEvent();
        boolean ifAllSelected = (event.getSelectedCount() == mCartRecyclerViewAdapter.getAllSubItemCount());
        ivSelectedAll.setChecked(ifAllSelected);
        updateSettleUI(event);
        updateTitleCountUI();
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onNotified(String message) {
        switch (message) {
            case "startDetailActivity":
                Intent intent = new Intent(this.getContext(), DetailActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void initStatusBar() {
        StatusBarUtil.setColor(this.getActivity(), getResources().getColor(R.color.topBarCart));
    }

    @Override
    public void setCommodities(List<List<ItemCommodity>> commodities) {
        mCartRecyclerViewAdapter.setCommodities(commodities);
        updateTitleCountUI();
    }

    private void updateTitleCountUI() {
        String string = String.format(getResources().getString(R.string.shop_title), mCartRecyclerViewAdapter.getCountAll() + "");
        tvTitleCount.setText(string);
    }

    private void clearUI() {
        mCartRecyclerViewAdapter.updateAllSelected(false);
        ivSelectedAll.setChecked(false);
    }
}
